Do
Do .. Loop
 
Parameters: NONE
Returns: NONE
 

      Do / Loop pairs in their most simplest form, are a way to create unconditional infinite loops. The Do represents the start or entry point of a section of code that is to be repeated, while the loop acts as the jump back to start instruction, telling the PlayBASIC to jump back to the preceding matching do instruction. So when a Do-Loop is executed, PlayBASIC will run any code placed inside the Do / Loop pair infinitely.


      Do / Loop pairs can also be used to created conditional loops by placing a conditional expression after either the Do or Loop statements. Placing an expression after the Do creates a While-EndWhile replacement, placing an expression after loop turns it into a Repeat- Until substitute.




FACTS:


      * Do / Loop's must be paired.

      * Do & Loop statements may contain conditional expressions (See example bellow).

      * The Do must proceed the Loop statement. You'll get an error otherwise.

      * None conditional Do / Loops will repeat a section of code forever, or until the Loop is broken, via Exit or ExitDo. Users can manually break the execution of the Do/Loop by pressing the ESCape key or clicking the Window Close [X] gadget in windowed program. It may take runtime enviroment a while to respond however, in locked up a program.

      * Not sure about loops ? Make sure you read the Loops tutorial then




Mini Tutorial #1:


      In this example, we see a Do-Loop being used to keep the program constantly running. This code will randomly draw dots to the screen. It will Do so, until the program is interrupted, by either the break key (ESC key) or clicking upon the close gadget on the window.


  
  
; Start DO/LOOP - This section of code will be executed in a LOOP
  Do
     
   ; Randomly draw a Dot on the screen
     DotC Rnd(800),Rnd(600),RndRGB()
     
   ; Show/Refresh the Screen to the user.
     Sync
     
   ; Loop back to the DO statement to keep this program running
  Loop
  
  


To examine more closely what's happening, lets step through the logic that this piece of code is telling PlayBASIC to perform.

  
  
;>> FIRST Loop
  Do     ; <<<<  Tell PB to remember this point in the program.
     DotC Rnd(800),Rnd(600),RndRGB()     ; <<<<  Draw Dot to the Screen at a random position
     Sync   ; <<<<  Tell PlayBASIC to display the screen to the user.
  Loop  ; <<<<  Tell PlayBASIC to jump back to the previous Do,
; and continue executing from there.
  
;>> SECOND Loop
  Do     ; <<<<  Tell PB to remember this point in the program.
     DotC Rnd(800),Rnd(600),RndRGB()     ; <<<<  Draw Dot to the Screen at a random position
     Sync   ; <<<<  Tell PlayBASIC to display the screen to the user.
  Loop  ; <<<<  Tell PlayBASIC to jump back to the previous Do
;, and continue executing from there.
  
;>> THIRD Loop
  Do     ; <<<<  Tell PB to remember this point in the program.
     DotC Rnd(800),Rnd(600),RndRGB()     ; <<<<  Draw Dot to the Screen at a random position
     Sync   ; <<<<  Tell PlayBASIC to display the screen to the user.
  Loop  ; <<<<  Tell PlayBASIC to jump back to the previous Do,
; and continue executing from there.
  
;>>  Etc etc.... Until program is stopped by the user.
  
  







Mini Tutorial #2:


      In this example, we see a conditional Do-Loop being used to keep the program constantly running. The program uses the computers Timer() value to execute the code within the Do-Loop structured for a period of 5 seconds (5000 milliseconds). Once that time is up, PlayBASIC exits the Do-Loop and continues on. Making a conditional Do-Loop the equivalent of While/EndWhile loop.


  
  
; First we create variable called 'EndTime'. This variable
; will hold the current timer() value in milliseconds
; plus 5000.  Since there's 1000 milliseconds per second, this
; means we're setting the variable to be 5 seconds ahead of
; now
  
  EndTime=Timer()+5000
  
; Start Conditional DO/LOOP.  The code inside this do/loop will
; only be executed if the current Timer() value is bellow our
; expected end value.  When this occurs the PlayBASIC will
; abort the Do/loop and continue on.
  
  Do Timer()<EndTime
     
   ; Randomly draw a Dot on the screen
     DotC Rnd(800),Rnd(600),RndRGB()
     
   ; Show/Refresh the Screen to the user.
     Sync
     
   ; Loop back to the DO statement to keep this program running
  Loop
  
  
  Print "Done - Press any key"
  
  Sync
  WaitKey
  
  
  



 
Related Info: Comparisons | Continue | DecLoop | Exit | ExitDo | For | Loop | Loops | Repeat | While :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com